home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / wais / waisgate / lock.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  6KB  |  244 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.    
  4.   
  5. */
  6.  
  7. #ifndef lint
  8. static char *RCSid = "$Header: /y/src/wais/wais-8-b5/ir/RCS/lock.c,v 1.4.1.1 1992/07/11 01:02:47 curtisg Exp curtisg $";
  9. #endif
  10.  
  11. /* Change log:
  12.  * $Log: lock.c,v $
  13.  * Revision 1.4.1.1  1992/07/11  01:02:47  curtisg
  14.  * Changes for SCO UNIX
  15.  *
  16.  * Revision 1.4  92/05/06  17:33:25  jonathan
  17.  * ?
  18.  * 
  19.  * Revision 1.3  92/04/28  17:05:23  jonathan
  20.  * K&R style definitions.
  21.  * 
  22.  */
  23.  
  24. /* ----------------------------------------------------------------------------
  25.  
  26.     COPYRIGHT    1992 Thinking Machines Corporation
  27.     AUTHOR        M. Tracy Shen
  28.                         Modified version of Gordon Linoff's lock.c
  29.     MODULE        lock.c -- handle lock files all in one place
  30.     INTERFACES
  31.             utlk_using_lock()   - check the existence of a lock file
  32.             utlk_set_lock()     - set a lock file if one doesn't exist
  33.             utlk_unset_lock()   - unset a lock file if one exists
  34.     INTERNAL ROUTINES
  35.     COMMENTS
  36. ---------------------------------------------------------------------------- */
  37.  
  38. #include <stdio.h>
  39. #include <sys/param.h>
  40. #include <errno.h>        /* errno, EWOULDBLOCK, etc. */
  41.  
  42. #ifdef Mach
  43. extern int errno;
  44. #endif
  45.  
  46. #define SIGNOP 0        /* for using kill() to check if process is
  47.                    alive */ 
  48.  
  49. #define FALSE 0
  50. #define TRUE  1
  51.  
  52.  
  53. #define LOCK_STORAGE_MODULE
  54. #include "lock.h"
  55. #undef LOCK_STORSGE_MODULE
  56.  
  57. #include "futil.h"
  58.  
  59. /******************************************************************************
  60.  *
  61.  *    Function Name:    utlk_using_lock_and_get_pid
  62.  *
  63.  *
  64.  *    Purpose:    check if update or query server is running
  65.  *
  66.  *    Returns:    return TRUE if process running, FALSE if not running
  67.  *            NOT_RUNNING.
  68.  *                      If pid is not NULL, sets it to the process id of
  69.  *                      locking process.
  70.  *
  71.  *    Algorithm:    Find a lock file in /tmp for update or query, read
  72.  *            pid from file, be sure process is running with that
  73.  *            pid.
  74.  *    End Algorithm
  75.  */
  76.  
  77. long
  78.     utlk_using_lock_and_get_pid (dbname, lock_type,pid_ptr)
  79. char *dbname;
  80. long    lock_type;
  81. unsigned *pid_ptr;
  82. {
  83.     /* local variables */
  84.     char lockfile_name[MAX_PATH_NAME_LEN];    /* absolute pathname of update lock */
  85.     FILE *file_ptr;            /* for opening lock files */
  86.     long pid;                /* pid of SC or update process */
  87.     long status;
  88.  
  89.     if (! IN_LOCK_RANGE(lock_type)) {
  90.     /* Out of range locks should never happen, but just in case . . . */
  91.     return(FALSE);
  92.         }
  93.  
  94.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  95.  
  96.     file_ptr = fopen(lockfile_name, "r"); /* look for an existing lock file */
  97.  
  98.     if (file_ptr == NULL) {     /* didn't find lock file! */
  99.     return(FALSE);
  100.        }
  101.  
  102.     /* read pid from the lock file */
  103.     status = fscanf(file_ptr, "%d", &pid);
  104.     fclose(file_ptr);
  105.     if (status == 0) {  /* is an empty lock file */
  106.     return(FALSE);
  107.         }
  108.  
  109.     /* Try a dummy kill; if ESRCH, process is not found.  Otherwise, assume
  110.      * it's there. */
  111.     status = kill(pid, SIGNOP);
  112.     if ((status != 0) && (errno == ESRCH)) {
  113.     return(FALSE);
  114.     }
  115.     if (pid_ptr != NULL) *pid_ptr = pid;
  116.     return(TRUE);
  117.  
  118.     }  /* end utlk_using_lock_and_get_pid */
  119.  
  120. /******************************************************************************
  121.  *
  122.  *    Function Name:    utlk_using_lock
  123.  *
  124.  *
  125.  *    Purpose:    check if update or query server is running
  126.  *
  127.  *    Returns:    return TRUE if process running, FALSE if not running
  128.  *            NOT_RUNNING.
  129.  *
  130.  *    Algorithm:    Find a lock file in /tmp for update or query, read
  131.  *            pid from file, be sure process is running with that
  132.  *            pid.
  133.  *    End Algorithm
  134.  */
  135.  
  136. long
  137.     utlk_using_lock (dbname, lock_type)
  138. char *dbname;
  139. long lock_type;
  140. {
  141.     return(utlk_using_lock_and_get_pid(dbname, lock_type, NULL));
  142. }  /* end utlk_using_lock */
  143.  
  144.  
  145.  
  146. /******************************************************************************
  147.  *
  148.  *    Function Name:    utlk_unset_lock
  149.  *
  150.  *
  151.  *    Purpose:    free lock
  152.  *
  153.  *    Returns:    Return TRUE is successful, FALSE if lock cannot be unset
  154.  *
  155.  *    Algorithm:    
  156.  *    End Algorithm
  157.  */
  158.  
  159. long
  160.     utlk_unset_lock (dbname, lock_type)
  161. char *dbname;
  162. long lock_type;
  163. {
  164.     /* local variables */
  165.     char lockfile_name[MAX_PATH_NAME_LEN];    /* absolute pathname of update lock */
  166.     
  167.     /* begin executable unset_lock */
  168.     
  169.     if (! IN_LOCK_RANGE(lock_type)) {
  170.     /* Out of range locks should never happen, but just in case . . . */
  171.     return(TRUE);
  172.     }
  173.     
  174.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  175.     
  176.     return(unlink(lockfile_name) == 0);
  177. }  /* end utlk_unset_lock */
  178.  
  179.  
  180.  
  181. /******************************************************************************
  182.  *
  183.  *    Function Name:    utlk_set_lock_with_pid
  184.  *
  185.  *
  186.  *    Purpose:    set lock, process id is argument
  187.  *
  188.  *    Returns:    Return TRUE is successful, FALSE if lock cannot be set
  189.  *
  190.  *    Algorithm:    
  191.  *    End Algorithm
  192.  */
  193.  
  194. long
  195.     utlk_set_lock_with_pid (dbname, lock_type, pid)
  196. char *dbname;
  197. long lock_type;            /* LOCK_UPDATE, etc. */
  198. long pid;            /* pid of SC or update process */
  199. {
  200.     /* local variables */
  201.     char lockfile_name[MAX_PATH_NAME_LEN];    /* absolute pathname of update lock */
  202.     FILE *file_ptr;            /* for opening lock files */
  203.  
  204.     /* begin executable set_lock */
  205.     
  206.     if (! IN_LOCK_RANGE(lock_type)) {
  207.     /* Out of range locks should never happen, but just in case . . . */
  208.     return(FALSE);
  209.     }
  210.     
  211.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  212.  
  213.     /* Don't look for an existing file -- just write over it */
  214.     file_ptr = fopen(lockfile_name, "w");
  215.     if (file_ptr == NULL) {    /* no write permission to db  */
  216.     return(FALSE);
  217.     }
  218.     fprintf(file_ptr, "%d", pid);
  219.     fclose(file_ptr);
  220.     return(TRUE);
  221. }  /* end utlk_set_lock */
  222.  
  223.  
  224. /******************************************************************************
  225.  *
  226.  *    Function Name:    utlk_set_lock
  227.  *
  228.  *
  229.  *    Purpose:    set lock
  230.  *
  231.  *    Returns:    Return TRUE is successful, FALSE if lock cannot be set
  232.  *
  233.  *    Algorithm:    
  234.  *    End Algorithm
  235.  */
  236.  
  237. long
  238.     utlk_set_lock (dbname, lock_type)
  239. char *dbname;
  240. long lock_type;
  241. {
  242.     return(utlk_set_lock_with_pid(dbname, lock_type, getpid()));
  243. }  /* end utlk_set_lock */
  244.